﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGServer.Contracts.Requests
{
    static class RequestLogin
    {
        /// <summary>
        /// 处理登录操作
        /// </summary>
        public static void Process(RPGPlayer player, byte[] buffer, int length)
        {
            int offset = 2;
            ushort verMajor = IntUtil.UShortFromBuffer(buffer, offset, out offset);
            ushort verMinor = IntUtil.UShortFromBuffer(buffer, offset, out offset);
            ushort verBuild = IntUtil.UShortFromBuffer(buffer, offset, out offset);
            string machineId = IntUtil.StringFromBuffer(buffer, offset, 64, out offset);
            string account = IntUtil.StringFromBuffer(buffer, offset, 32, out offset);
            string password = IntUtil.StringFromBuffer(buffer, offset, 32, out offset);

            Log.Debug("request", "玩家({0})请求登录[version:v{1}.{2}.{3} machineid:{4} account:{5} password:{6}]",
                player.Client.GetNetAddress(),
                verMajor,
                verMinor,
                verBuild,
                machineId,
                account,
                password);

            // 验证用户是否符合要求
            bool validateResult = true;
            if (validateResult)
            {
                player.Id = PlayerService.getPlayerId();
                byte[] data = new byte[255];
                offset = 0;
                // scenario
                IntUtil.ByteToBuffer(RPGNET_SCENARIO.INIT, data, offset, out offset);
                // cmd
                IntUtil.ByteToBuffer(RPGNET_RESPONSE.LOGIN, data, offset, out offset);
                // error code
                IntUtil.ByteToBuffer(RPGNET_RESPONSE_ERROR.NOERROR, data, offset, out offset);
                // player id
                IntUtil.UIntToBuffer(player.Id, data, offset, out offset);
                // player name
                IntUtil.StringToBuffer(String.Format("player{0}", player.Id), data, offset, 32, out offset);
                // object count
                IntUtil.ByteToBuffer(0x01, data, offset, out offset);
                // left object id
                IntUtil.UIntToBuffer(player.Id, data, offset, out offset);
                // left model id
                IntUtil.UShortToBuffer(8001, data, offset, out offset);
                // left object name
                IntUtil.StringToBuffer(String.Format("object{0}", player.Id), data, offset, 32, out offset);
                // left object level
                IntUtil.ByteToBuffer(0x01, data, offset, out offset);
                // left object mapid
                IntUtil.UShortToBuffer(0x02, data, offset, out offset);
                // left object map pos-row
                IntUtil.UShortToBuffer(0x02, data, offset, out offset);
                // left object map pos-col
                IntUtil.UShortToBuffer(0x02, data, offset, out offset);
                // left object direction
                IntUtil.ByteToBuffer(0x00, data, offset, out offset);

                // 告诉玩家登录成功
                player.Client.SendData(data, offset);
                Log.Debug("response", "玩家({0})登录成功:编号({1})", player.Client.GetNetAddress(), player.Id);
            }
            else
            {
                byte[] data = new byte[255];
                offset = 0;
                // scenario
                IntUtil.ByteToBuffer(RPGNET_SCENARIO.INIT, data, offset, out offset);
                // cmd
                IntUtil.ByteToBuffer(RPGNET_RESPONSE.LOGIN, data, offset, out offset);
                // error code
                IntUtil.ByteToBuffer(RPGNET_RESPONSE_ERROR.UNKNOWN, data, offset, out offset);

                player.Client.SendData(data, offset);
                Log.Debug("response", "玩家({0})登录失败!", player.Client.GetNetAddress());
            }
        }
    }
}
